package com.smedi.ismedi.web.starter.config;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;

import static com.smedi.ismedi.authority.starter.constant.AuthorityConstant.ISMEDI_ACCESS_INFO;
import static com.smedi.ismedi.authority.starter.constant.AuthorityConstant.ISMEDI_ACCESS_TOKEN;
import static com.smedi.ismedi.authority.starter.context.CurrentUserHelper.infoContext;
import static com.smedi.ismedi.authority.starter.context.CurrentUserHelper.tokenContext;

/**
 * 解决feign调用外部请求时header丢失
 * 通过配置文件设置头信息进行绑定
 */
@Slf4j
@Configuration
@AllArgsConstructor
public class FeignHeaderForwardConfig implements RequestInterceptor {
    // 头参数配置
//    private final FeignHeaderForwardProperty feignHeaderForwardProperty;

    @Override
    public void apply(RequestTemplate requestTemplate) {
        String token = tokenContext.get();
        String info = infoContext.get();
        requestTemplate.header(ISMEDI_ACCESS_TOKEN, token);
        requestTemplate.header(ISMEDI_ACCESS_INFO, info);


//        ServletRequestAttributes attributes = null;
//        try {
//            attributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
//        }catch (Exception e){
//
//        }
//        if(null == attributes || null == attributes.getRequest().getSession()){
//            return;
//        }
//        HttpServletRequest request = attributes.getRequest();
//        HttpServletResponse response = attributes.getResponse();
//
//        // 设置返回中文
//        response.setHeader("Content-type", "text/html;charset=UTF-8");
//        response.setCharacterEncoding("UTF-8");
//
//        // 添加Request Header
//        Map<String, Object> headerMaps = new HashMap<>();
//        new ArrayList<String>() {{
//            add(WebParameterConstant.prefix); // 标准头
//            if (feignHeaderForwardProperty.getPrefix() != null) {
//                addAll(Arrays.asList(feignHeaderForwardProperty.getPrefix()));
//            } // 额外添加的标准头
//        }}.stream().forEach(prefix -> {
//            Enumeration<String> headerNames = request.getHeaderNames();
//            while (headerNames.hasMoreElements()) {
//                String attribute = headerNames.nextElement();
//                String value = request.getHeader(attribute);
//
//                if (attribute.contains(prefix)) {
//                    requestTemplate.header(attribute, value);
//                    headerMaps.put(attribute, value);
//                }
//            }
//        });
//
//        // 添加请求Session参数至头
//        Map<String, Object> requestMaps = new HashMap<>();
//        new ArrayList<String>() {{
//            add(WebParameterConstant.prefix); // 标准头
//        }}.stream().forEach(prefix -> {
//            Enumeration<String> enumerations = request.getSession().getAttributeNames();
//            while (enumerations.hasMoreElements()) {
//                String attribute = enumerations.nextElement();
//                if (attribute.contains(prefix)) {
//                    String value = request.getSession().getAttribute(attribute).toString();
//                    String key = attribute.contains(WebParameterConstant.headerTag) ? attribute : (attribute + WebParameterConstant.headerTag);
//
//                    requestTemplate.header(key, value);
//                    requestMaps.put(attribute.replaceAll(WebParameterConstant.headerTag, ""), value);
//                }
//            }
//        });
//
//        // 设置 ticket 票据
////        Object attribute = request.getSession().getAttribute(AuthorityConstant.token);
////        if (attribute != null) {
////            requestTemplate.header(AuthorityConstant.token, String.valueOf(attribute));
////            maps.put(AuthorityConstant.token, String.valueOf(attribute));
////        }
//
//        // 打印日志
//        if (feignHeaderForwardProperty.isPrintLog()) {
//            log.info("feign请求头参数转发，拦截路由 >>> {}", request.getRequestURI());
//            for (Map.Entry entry : headerMaps.entrySet()) {
//                log.info(" ---- {}: {}", entry.getKey(), entry.getValue());
//            }
//
//            log.info("feign请求Request Session 参数转发，拦截路由 >>> {}", request.getRequestURI());
//            for (Map.Entry entry : requestMaps.entrySet()) {
//                log.info(" ---- {}: {}", entry.getKey(), entry.getValue());
//            }
//        }
    }
}
